<?php
namespace app\article\model;
use think\Db;
use think\Controller;

class ArticlesCategory extends Controller
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
        
        $categoryListData = [];
        $categoryAllListData = [];
        $categoryAllListData = db($this->itemCategory)->order('sort asc')->select();
        if ($categoryAllListData) {
            foreach ($categoryAllListData as $key => $value) {
                $categoryListData[$value['id']] = $value;
            }
        }
        $this->categoryListData = $categoryListData;
        $this->categoryAllListData = $categoryAllListData;
    }
    
    public function categoryAdd($data)
    {
        db($this->itemCategory)->insert(array(
            'title' => trim($data['title']),
            'url_name' =>  $data['url_name'] ? $data['url_name'] : getPinyin(trim($data['title'])),
            'seo_title' =>  $data['seo_title'],
            'template' => $data['template'] ? $data['template'] : 'article.html',
            'detail_template' => $data['detail_template'] ? $data['detail_template'] : 'articleDetail.html',
            'category_url' =>  $data['category_url'] ? $data['category_url'] : '/<url_name>/',
            'category_page_url' => $data['category_page_url'] ? $data['category_page_url'] : '<category_url>index_<page>.html',
            'detail_url' => $data['detail_url'] ? $data['detail_url'] : '<category_url><id>.html',
            'keywords' => $data['keywords'],
            'description' => $data['description'],
            'status' => isset($data['status']) ? $data['status'] : 1,
            'is_page' => $data['is_page'] ? 1 : 0,
            'content' => htmlspecialchars($data['content']),
            'pid' => isset($data['pid']) ? $data['pid'] : 0, 
            'model_id' => 0, 
            'model_name' => $data['modelName'] ? $data['modelName'] : 'article',
        ));
        return jsonSuccess('添加成功');
    }
    
    public function categoryEdit($data)
    {
        db($this->itemCategory)->where('id',$data['id'])->update(array(
            'title' => $data['title'],
            'url_name' => $data['url_name'],
            'seo_title' => $data['seo_title'],
            'keywords' => $data['keywords'],
            'description' => $data['description'],
            'template' => $data['template'],
            'detail_template' => $data['detail_template'],
            'category_url' => $data['category_url'],
            'category_page_url' => $data['category_page_url'],
            'detail_url' => $data['detail_url'],
            'status' => isset($data['status']) ? $data['status'] : 1,
            'is_page' => $data['is_page'] ? 1 : 0,
            'content' => htmlspecialchars($data['content']),
            'pid' => $data['pid'] ? $data['pid'] : 0,
            'model_id' => 0, 
            'model_name' => $data['modelName'],
            ));
        return true;
    }
     
    public function getCategoryInfo($id)
    {
        if (!$id) {
            $itemCategoryInfo['url'] = config('domain') . '/'. $this->itemType . '/';
            $itemCategoryInfo['id'] = 0;
            $itemCategoryInfo['title'] = $this->itemName;
            $itemCategoryInfo['url_name'] = $this->itemType;
            return $itemCategoryInfo;
        }
        $itemCategoryInfo = $this->categoryListData[$id];
        if (!$itemCategoryInfo) {
            return false;
        }
        
        if ($itemCategoryInfo['pid'] == 0) {
            $urlName = $itemCategoryInfo['url_name'];
        } else {
            $tempCategoryInfo = $this->getCategoryInfo($itemCategoryInfo['pid']);
            $itemCategoryInfo['parent'] = $tempCategoryInfo;
            if ($tempCategoryInfo) {
                $urlName = $tempCategoryInfo['url_name'] . '/' . $itemCategoryInfo['url_name'];
            }
        }
        
        if ($itemCategoryInfo['category_url']) {
            $categoryUrl = $itemCategoryInfo['category_url'];
            $categoryUrl = str_replace('<url_name>',$urlName,$categoryUrl);
            $categoryUrl = str_replace('<id>',$itemCategoryInfo['id'],$categoryUrl);
            $itemCategoryInfo['rule'] = $categoryUrl;
            $itemCategoryInfo['url'] = config('domain') . $categoryUrl;
        }
        if ($itemCategoryInfo['category_page_url']) {
            $categoryPageUrl = $itemCategoryInfo['category_page_url'];
            $categoryPageUrl = str_replace('<category_url>', $categoryUrl,$categoryPageUrl);
            $categoryPageUrl = str_replace('<url_name>',$itemCategoryInfo['url_name'],$categoryPageUrl);
            $categoryPageUrl = str_replace('<id>',$itemCategoryInfo['id'],$categoryPageUrl);
            if (strpos($categoryPageUrl,'.html')) {
                $categoryPageUrl = str_replace('.html','',$categoryPageUrl);
            }
            $itemCategoryInfo['pageRule'] = $categoryPageUrl;
        }
        if ($itemCategoryInfo['detail_url']) {
            $detailUrl = $itemCategoryInfo['detail_url'];
            if (strpos($categoryUrl,'.html')) {
                $categoryUrl = str_replace('.html','/',$categoryUrl);
            }
            $detailUrl = str_replace('<category_url>', $categoryUrl,$detailUrl);
            $itemCategoryInfo['detailRule'] = $detailUrl;
        }
        if ($detailUrl) {
            $detailUrl = str_replace('.html','',$detailUrl);
            $tempDetailUrl = substr($detailUrl, 0, 1) == '/' ? substr($detailUrl, 1) : $detailUrl;
            $itemCategoryInfo['detail__url__'] = str_replace('/','\/',str_replace('<id>','[a-zA-Z0-9_-]+$',$tempDetailUrl));
        }
        $itemCategoryInfo['content'] = htmlspecialchars_decode($itemCategoryInfo['content']);
        
        $itemCategoryInfo['value'] = $itemCategoryInfo['id'];
        $itemCategoryInfo['label'] = $itemCategoryInfo['title'];
        
        return $itemCategoryInfo;
    }

    public function getCategory($paramData = [])
    {
    	if (!$paramData) {
    		$pid = '';
    	} else {
			$pid = isset($paramData['pid']) ? $paramData['pid'] : 0;
    	}
        $page = $paramData['page'] ? $paramData['page'] : 1;
        $limit = $paramData['limit'] ? $paramData['limit'] : '';
        $orderBy = $paramData['orderBy'] ? $paramData['orderBy'] : 'sort';
        $order = $paramData['order'] ? $paramData['order'] : 'asc';
        $ids = $paramData['ids'] ? $paramData['ids'] : '';
        $whereArray = $paramData['whereArray'] ? $paramData['whereArray'] : '';
        $isAll = $paramData['isAll'] ? $paramData['isAll'] : '';
    	if (empty($ids)) {
            $idsWhere = null;
        } else {
            $ids = explode(',',$ids);
            $idsWhere['id']  = ['in',$ids];
        }
		$itemCategoryList = array();
        if ($ids) {
        	if (is_array($ids)) {
        		if ($this->categoryAllListData) {
	                foreach ($this->categoryAllListData as $key => $val) {
	                    if (in_array($val['id'],$ids)) {
	                        if ($val['status'] == 1) {
	                            $itemCategoryList[] = $val;
	                        }
	                    }
	                }
	            }
        	} else {
	        	if ($this->categoryAllListData) {
	                foreach ($this->categoryAllListData as $key => $val) {
	                    if ($val['id'] == $ids) {
	                        if ($val['status'] == 1) {
	                            $itemCategoryList[] = $val;
	                        }
	                    }
	                }
	            }
        	}
        } else {
	        if ($pid === '') {
	            $itemCategoryList = $this->categoryAllListData;
	        } else {
	            if ($this->categoryAllListData) {
	                foreach ($this->categoryAllListData as $key => $val) {
	                    if ($val['pid'] == $pid) {
	                        if ($isAll) {
	                            $itemCategoryList[] = $val;
	                        } else {
	                            if ($val['status'] == 1) {
	                                $itemCategoryList[] = $val;
	                            }
	                        }
	                    }
	                }
	            }
	        }
        }
        if ($whereArray) {
            $itemCategoryList  = db($this->itemCategory)->where('pid',$pid)->where($idsWhere)->where($whereArray)->order($orderBy,$order)->select();
        }
        if ($limit) {
            $itemCategoryList = array_slice($itemCategoryList,0,$limit);
        }
        if($itemCategoryList) {
            foreach ($itemCategoryList as $key => $val) {
                $itemCategoryList[$key] = $this->getCategoryInfo($val['id']);
                $paramData['pid'] = $val['id'];
                $paramData['ids'] = '';
                $itemCategoryList[$key]['sub'] = $this->getCategory($paramData);
                $itemCategoryList[$key]['children'] = $itemCategoryList[$key]['sub'];
            }
        }

        return $itemCategoryList;
    }
    
}